package com.ourfox.a4.commons.services;

import java.util.Collection;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ourfox.a4.commons.constants.ErrorCode;
import com.ourfox.a4.commons.dao.GenericDao;
import com.ourfox.a4.commons.exception.PortalException;

/**
 * 
 * @Description: 所有service基类
 * @author xiujin.luo@gmail.com
 * @date 2013-4-11 上午2:15:36 
 * @version V1.0.0
 * @param <PO>
 * @param <ID>
 * @param <DAO>
 */
public abstract class GenericServiceImpl<PO, ID, DAO extends GenericDao<PO, ID>>
		implements GenericService<PO, ID, DAO> {

	protected static final Log logger = LogFactory.getLog(GenericServiceImpl.class);
	
	public int add(PO po) throws PortalException {
		if(po == null){
			logger.error("Params is null");
			throw new PortalException(ErrorCode.PARAMS_IS_NULL);
		}
		return this.getDAO().add(po);
	}

	public int add(Collection<PO> poList) throws PortalException {
		if(poList == null){
			logger.error("Params is null");
			throw new PortalException(ErrorCode.PARAMS_IS_NULL);
		}
		return this.getDAO().addBatch(poList);
	}

	public void update(PO po) throws PortalException {
		if(po == null){
			logger.error("Params is null");
			throw new PortalException(ErrorCode.PARAMS_IS_NULL);
		}
		this.getDAO().update(po);
		
	}

	public void update(Collection<PO> poList) throws PortalException {
		if(poList == null){
			logger.error("Params is null");
			throw new PortalException(ErrorCode.PARAMS_IS_NULL);
		}
		this.getDAO().updateBatch(poList);
	}

	public void removeById(ID id) throws PortalException {
		
		this.getDAO().removeById(id);
		
	}

	public void remove(PO po) throws PortalException {
		if(po == null){
			logger.error("Params is null");
			throw new PortalException(ErrorCode.PARAMS_IS_NULL);
		}
		this.getDAO().remove(po);
	}

	public void remove(Collection<ID> idList) throws PortalException {
		if(idList == null){
			logger.error("Params is null");
			throw new PortalException(ErrorCode.PARAMS_IS_NULL);
		}
		this.getDAO().removeBatch(idList);
	}

	public PO get(ID id) throws PortalException {
		return this.getDAO().get(id);
	}

	public List<PO> query(PO po, int startRow, int rowCount)
			throws PortalException {
		return this.query(po, startRow, rowCount);
	}

	public int getCount(PO po) throws PortalException {
		return this.getCount(po);
	}

}
